<template>
  <scroll class="listview"
          :data="list"
          ref="listview"
          :probeType="probeType"
          :scrollListener="scrollListener"
          @scroll="scroll"
  >
    <ul>
      <li v-for="group in list" class="list-group" ref="listgroup">
        <h2 class="list-group-title">{{group.title}}</h2>
        <ul>
          <li v-for="item in group.items" class="list-group-item" @click="selectItem(item)">
            <img class="avatar" v-lazy="item.avatar"/>
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
    <div class="list-shortcut" @touchstart="shortcutTouchStart" @touchmove="shortcutTouchMove">
      <ul>
        <li
          v-for="(item,index) in shortCutList"
          class="item"
          :data-index="index"
          :class="{'current':index===currentIndex}"
        >{{item}}
        </li>
      </ul>
    </div>
    <div class="list-fixed"
         v-show="fixedTitle"
         ref="fixedTitle">{{fixedTitle}}</div>
    <div v-show="!list.length" class="loading-wrapper">
      <loading></loading>
    </div>
  </scroll>
</template>

<script>
  import Scroll from 'base/scroll/scroll'
  import {getData} from "common/js/dom"
  import Loading from 'base/loading/loading'

  const ITEM_HEIGHT = 18
  const TITLE_HEIGHT = 30

  export default {
    name: "listview",
    props: {
      list: {
        type: Array,
        default: []
      }
    },
    computed: {
      shortCutList() {
        return this.list.map((group) => {
          return group.title.substring(0, 1)
        })
      },
      fixedTitle() {
        if (this.scrollY > 0) {
          return ''
        }
        return this.list[this.currentIndex] ? this.list[this.currentIndex].title : ''
      }
    },
    components: {
      Scroll,Loading
    },
    data() {
      return {
        scrollY: -1,
        currentIndex: 0,
        diffY: -1
      }
    },
    created() {
      this.touch = {},
        this.probeType = 3,
        this.scrollListener = true,
        this.listHeight = []
    },
    watch: {
      list() {
        setTimeout(() => {
          this._calculateHeight()
        }, 20)
      },
      scrollY(newY) {
        let listHeight = this.listHeight;
        //当滚动到顶部newY>0
        if (newY > 0) {
          this.currentIndex = 0;
          return
        }
        //中间部分滚动
        for (let i = 0; i < listHeight.length - 1; i++) {
          let height1 = listHeight[i]
          let height2 = listHeight[i + 1]
          if (Math.abs(newY) >= height1 && Math.abs(newY) < height2) {
            this.currentIndex = i;
            this.diffY = height2 + newY;
            return
          }
        }
        //当滚动到底部,并且newY大于height2
        this.currentIndex = listHeight.length - 2;
      },
      diffY(newDiff) {
        const top = newDiff > 0 && newDiff < TITLE_HEIGHT ? newDiff - TITLE_HEIGHT : 0
        if (top === this.top) {
          return
        }
        this.top = top
        console.log(top)
        this.$refs.fixedTitle.style.transform = `translate3d(0,${top}px,0)`
      }
    },
    methods: {
      selectItem(item){
        this.$emit('select',item)
      },
      shortcutTouchStart(e) {
        let shortcutIndex = getData(e.target, 'index')
        this.touch.touchY = e.touches[0].pageY
        this.touch.shortcutIndex = shortcutIndex
        this._scrollTo(shortcutIndex)
      },
      shortcutTouchMove(e) {
        let currentY = e.touches[0].pageY
        let touchY = this.touch.touchY
        let delY = currentY - touchY;
        let currentIndex = parseInt(this.touch.shortcutIndex) + Math.floor(delY / ITEM_HEIGHT)
        this._scrollTo(currentIndex)
      },
      scroll(pos) {
        this.scrollY = pos.y
      },

      _scrollTo(index) {

        if (!index) {
          return
        }
        if (index < 0) {
          index = 0
        } else if (index > this.listHeight.length - 2) {
          index = this.listHeight.length - 2
        }
        // this.currentIndex = index 这里dom不会刷新
        this.scrollY = -this.listHeight[index]
        this.$refs.listview.scrollElement(this.$refs.listgroup[index], 0)
      },

      _calculateHeight() {
        //list数据变化时计算高度
        this.listHeight = []
        let listHeight = this.listHeight
        let listgroup = this.$refs.listgroup
        let height = 0
        listHeight.push(height)
        for (let i = 0; i < listgroup.length; i++) {
          let item = listgroup[i];
          height += item.clientHeight
          listHeight.push(height)
        }
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  .listview
    width 100%
    position relative
    height 100%
    overflow hidden
    background $color-background
    .list-group
      padding-bottom 30px
      .list-group-title
        height 30px
        line-height 30px
        padding-left 20px
        font-size 12px
        color $color-text-l
        background $color-highlight-background
      .list-group-item
        padding 20px 0 0 30px
        display flex
        align-items center
        .avatar
          width 50px
          height 50px
          border-radius 50% s
        .name
          margin-left 20px
          font-size 14px
          color $color-text-l
    .list-shortcut
      position absolute
      right 0
      top 50%
      transform translateY(-50%)
      z-index 30
      padding 20px 0
      width 20px
      text-align center
      background $color-background-d
      border-radius 10px
      .item
        font-size $font-size-small
        color $color-text-l
        padding 3px
        line-height 1
        &.current
          color $color-theme
    .list-fixed
      position absolute
      top 0
      left 0
      width 100%
      height 30px
      line-height 30px
      padding-left 20px
      font-size 12px
      color $color-text-l
      background $color-highlight-background
    .loading-wrapper
      position absolute
      width 100%
      top 50%
      transform translateY(-50%)
</style>
